package interfaces.menu.options;

import java.util.ArrayList;
import java.util.List;

import interfaces.menu.AbsMenu;
import interfaces.menu.MenuMessageWindow;
import interfaces.options.KeyOptionWidget;
import interfaces.options.OptionWidget;
import interfaces.options.OptionsContainer;

import logic.nodes.nodeSettings.Settings;
import main.InitGame;

import settings.ControlSettings;

import fileHandling.ProfileHandler;
import fileHandling.language.Lang;
import fileHandling.language.LanguageLoader;
import fileHandling.language.interfaces.OptionsWindowText;
import fileHandling.language.options.Controls;
import gameStates.absGamesStates.gui.AbsMenuState;

public class ControlOptionsContent extends OptionContent {
	
	private List<KeyOptionWidget> keyOptions;
	private List<KeyOptionWidget> doubleKeyWidgets;
	private OptionsContainer commonOptions, pilotOptions;
	
	public ControlOptionsContent(OptionsMenu optionsMenu, AbsMenu menu) {
		super(optionsMenu, menu, "controls.xml", ControlSettings.get());
		
		keyOptions = new ArrayList<KeyOptionWidget>();
		doubleKeyWidgets = new ArrayList<KeyOptionWidget>();
		
		commonOptions = createOptionsContainer(OptionsWindowText.Common, 
				Controls.Forward, Controls.Break, Controls.Left, Controls.Right, 
				Controls.Spawn_Menu, Controls.Scoreboard, Controls.Show_Map, Controls.Inverted_Mouse, 
				Controls.Chat, Controls.Team_Chat, Controls.Squad_Chat, Controls.Pause);
		
		pilotOptions = createOptionsContainer(OptionsWindowText.Pilot, 
				Controls.Nearest_Target, Controls.Activate_Boost, Controls.Fire_Energy_Weapons, Controls.Fire_Heatseaker, 
				Controls.Fire_Missile, Controls.Use_Counter_Measures, Controls.Use_Repairkit, Controls.Use_Shieldbattery, 
				Controls.Use_Energycell, Controls.Toggle_Mission, Controls.Move_To_Mission_Location, 
				Controls.Hunter_Rotation_Reset, Controls.Look_Around);
	}
	
	@Override
	protected void addOptionWidget(OptionWidget widget, OptionsContainer container) {
		super.addOptionWidget(widget, container);
		if(widget instanceof KeyOptionWidget) {
			keyOptions.add((KeyOptionWidget)widget);
		}
	}
	
	@Override
	protected void apply() {
		AbsMenuState menuState = menu.getMenuState();
		
		if(!doubleKeyWidgets.isEmpty()) {
			String doubleKey = LanguageLoader.get(OptionsWindowText.Double_Key_found);
			String label = LanguageLoader.get(doubleKeyWidgets.get(0).getDescription());
			String message = doubleKey + ": " + label;
			
			MenuMessageWindow messageWindow = new MenuMessageWindow(menuState, doubleKey, message);
			menuState.addWidget(messageWindow);
		} else {
			ControlSettings settings = ControlSettings.get();
			settings.copySettings(new Settings(commonOptions), true);
			settings.copySettings(new Settings(pilotOptions), true);
			ProfileHandler.changeSettings(ControlSettings.get());
			InitGame.get().changeControlSettings();
		}
	}
	
	public void addDoubleKey(Lang description) {
		for(KeyOptionWidget keyOption : keyOptions) {
			if(keyOption.getDescription().equals(description)) {
				keyOption.changeKey("");
				doubleKeyWidgets.add(keyOption);
				return;
			}
		}
	}
	
	public void changeKeyBinding(KeyOptionWidget keyWidget, String newValue) {
		keyWidget.changeKey(newValue);
		if(doubleKeyWidgets.contains(keyWidget)) doubleKeyWidgets.remove(keyWidget);
	}
	
	public Lang getDoubleKeyDescription(String key) {
		for(OptionWidget option : keyOptions) {
			if(key.equals(option.getValue())) return option.getDescription();
		}
		return null;
	}
}